<?php
/*******************************************************************************
 *
 *  Name: 
 *
 *  Date: 
 *  
 *  Description: 
 *  
 *  Author: 
 *  
 *  Todo:
 *     NONE
 *
 *  Modification History:  
 * 
 *   Date         Name
 *  ----------   --------------
 *  2012-04-25   HAYDEN WAISANEN
 *  2012-04-10   HAYDEN WAISANEN
 *  2012-04-09   HAYDEN WAISANEN
 * 
 *****************************************************************************/
?>
<?php
class User_model extends CI_Model {

    var $UserID = '';
    var $Email     = '';
    var $Password  = '';
    var $IsManager = 0;

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->database();
    }
    
    function getUser($UserID)
    {
        $query = $this->db->get_where('User', array('UserID' => $UserID));
        return $query->result_array();
    }
    function getAllUsers()
    {
       //$query = $this->db->get('User');
        $querystring = 
       "SELECT *,
CASE ( SELECT COUNT(*) FROM Analyst WHERE Analyst.UserID = User.UserID ) 
WHEN 0 THEN '0' 
ELSE '1' 
END AS Analyst,
CASE ( SELECT COUNT(*) FROM IRTeam WHERE IRTeam.UserID = User.UserID ) 
WHEN 0 THEN '0' 
ELSE '1' 
END AS IRTeam,
CASE ( SELECT COUNT(*) FROM CustomerRep WHERE CustomerRep.UserID = User.UserID ) 
WHEN 0 THEN '0' 
ELSE '1' 
END AS CustomerRep
FROM User;";
        $query = $this->db->query($querystring);

       return $query->result_array();
    }

    function insert($fields)
    {
        //Check if it already exists
        $query = $this->db->get_where('User', array('UserID' => $fields['UserID']));
        if( $query->num_rows() > 0 )
           return false;

        $this->UserID   = $fields['UserID'];
        $this->Email   = $fields['Email']; 
        $this->Password = $fields['Password'];
        $this->IsManager    = $fields['IsManager'];

        $this->db->insert('User', $this);
        return true;
    }

    function update($fields)
    {
        $this->UserID   = $fields['UserID'];
        $this->Email   = $fields['Email']; 
        $this->Password = $fields['Password'];
        $this->IsManager    = $fields['IsManager'];
        $this->db->update('User', $this, array('UserID' => $fields['UserID']));
    }
    function delete($fields)
    {
         $this->db->where('UserID', $fields['UserID']);
         $this->db->delete('User'); 
    }
}
?>
